{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "76497abf-3545-4533-a0d9-569e8f8e8fd5",
   "metadata": {},
   "source": [
    "Chapter 06\n",
    "# 利用math.sin() 计算数列列表正弦值并可视化\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "71768578-076d-4003-8824-16cbcd119e1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import math\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "39d0b827-1352-4dfc-91ad-4eb7e9d8e07a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n"
     ]
    }
   ],
   "source": [
    "# 计算正弦值\n",
    "x_start = 0 # 弧度值\n",
    "print(math.sin(x_start))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ca9c9955-7641-48be-9c57-3bd1a4646a5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-2.4492935982947064e-16\n"
     ]
    }
   ],
   "source": [
    "x_end   = 2*math.pi # 弧度值\n",
    "print(math.sin(x_end))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f34eb037-f845-4804-b02f-0f15d1d4ec9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 等差数列 a_n = a_1 + d(n – 1)\n",
    "# 数列元素数量\n",
    "num     = 37\n",
    "# 计算公差\n",
    "step = (x_end - x_start) / (num - 1)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6371b4e8-d868-4196-9832-2fa6d31551b4",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.0,\n",
       " 0.17453292519943295,\n",
       " 0.3490658503988659,\n",
       " 0.5235987755982988,\n",
       " 0.6981317007977318,\n",
       " 0.8726646259971648,\n",
       " 1.0471975511965976,\n",
       " 1.2217304763960306,\n",
       " 1.3962634015954636,\n",
       " 1.5707963267948966,\n",
       " 1.7453292519943295,\n",
       " 1.9198621771937625,\n",
       " 2.0943951023931953,\n",
       " 2.2689280275926285,\n",
       " 2.443460952792061,\n",
       " 2.6179938779914944,\n",
       " 2.792526803190927,\n",
       " 2.96705972839036,\n",
       " 3.141592653589793,\n",
       " 3.316125578789226,\n",
       " 3.490658503988659,\n",
       " 3.665191429188092,\n",
       " 3.839724354387525,\n",
       " 4.014257279586958,\n",
       " 4.1887902047863905,\n",
       " 4.363323129985823,\n",
       " 4.537856055185257,\n",
       " 4.71238898038469,\n",
       " 4.886921905584122,\n",
       " 5.061454830783555,\n",
       " 5.235987755982989,\n",
       " 5.410520681182422,\n",
       " 5.585053606381854,\n",
       " 5.759586531581287,\n",
       " 5.93411945678072,\n",
       " 6.1086523819801535,\n",
       " 6.283185307179586]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成等差数列列表\n",
    "x_array = [x_start + i * step for i in range(num)]\n",
    "x_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "97adcdf9-75bc-4cef-a6a1-b1f038565bc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成等长全0列表,等价于zero_array = [0] * len(x_array)\n",
    "zero_array = [0 for i in range(num)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0a18c388-70c5-4ae3-8d9d-c9145ca665d9",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'x_array' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[8], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 可视化等差数列\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m plt\u001b[38;5;241m.\u001b[39mplot(x_array, zero_array, marker \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m      3\u001b[0m          markersize \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m8\u001b[39m,\n\u001b[0;32m      4\u001b[0m          markerfacecolor\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mw\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m      5\u001b[0m          markeredgecolor\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mk\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      6\u001b[0m plt\u001b[38;5;241m.\u001b[39mtext(x_start, \u001b[38;5;241m0\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m0\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      7\u001b[0m plt\u001b[38;5;241m.\u001b[39mtext(x_end,   \u001b[38;5;241m0\u001b[39m, \u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m$2\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mpi$\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'x_array' is not defined"
     ]
    }
   ],
   "source": [
    "# 可视化等差数列\n",
    "plt.plot(x_array, zero_array, marker = '.',\n",
    "         markersize = 8,\n",
    "         markerfacecolor=\"w\",\n",
    "         markeredgecolor='k')\n",
    "plt.text(x_start, 0, '0')\n",
    "plt.text(x_end,   0, r'$2\\pi$')\n",
    "plt.axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d9633f42-cdf7-4df9-8fab-63415d2a9a29",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'x_array' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[6], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 正弦 sin(x) 列表\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m y_array \u001b[38;5;241m=\u001b[39m [math\u001b[38;5;241m.\u001b[39msin(x_idx) \u001b[38;5;28;01mfor\u001b[39;00m x_idx \u001b[38;5;129;01min\u001b[39;00m x_array]\n\u001b[0;32m      3\u001b[0m y_array\n",
      "\u001b[1;31mNameError\u001b[0m: name 'x_array' is not defined"
     ]
    }
   ],
   "source": [
    "# 正弦 sin(x) 列表\n",
    "y_array = [math.sin(x_idx) for x_idx in x_array]\n",
    "y_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d0b88942-aa43-4aab-99e6-13a3b5095cc2",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'x_array' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 可视化正弦函数\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m plt\u001b[38;5;241m.\u001b[39mplot(x_array, y_array, marker \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m      3\u001b[0m          markersize \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m8\u001b[39m,\n\u001b[0;32m      4\u001b[0m          markerfacecolor\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mw\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m      5\u001b[0m          markeredgecolor\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mk\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      6\u001b[0m plt\u001b[38;5;241m.\u001b[39mtext(x_start, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m0.1\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m0\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      7\u001b[0m plt\u001b[38;5;241m.\u001b[39mtext(x_end,    \u001b[38;5;241m0.1\u001b[39m, \u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m$2\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mpi$\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'x_array' is not defined"
     ]
    }
   ],
   "source": [
    "# 可视化正弦函数\n",
    "plt.plot(x_array, y_array, marker = '.',\n",
    "         markersize = 8,\n",
    "         markerfacecolor=\"w\",\n",
    "         markeredgecolor='k')\n",
    "plt.text(x_start, -0.1, '0')\n",
    "plt.text(x_end,    0.1, r'$2\\pi$')\n",
    "plt.axhline(y=0, color='k', linestyle='--', linewidth=0.25)\n",
    "plt.axis('off') \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53aa68b1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
